import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginView extends JFrame {
    public LoginView() {
        //控件
        JLabel l_title = new JLabel("登录页面");
        JLabel l_username = new JLabel("用户名");
        JLabel l_password = new JLabel("密码");
        JTextField t_username = new JTextField();
        JTextField t_password = new JPasswordField();
        //按钮
        JButton reset = new JButton("清空");
        JButton login = new JButton("登录");
        //JPanel
        JPanel panel = new JPanel();
        panel.setLayout(null);
        //设置控件大小
        l_title.setBounds(150, 20, 100, 50);
        l_username.setBounds(100, 80, 100, 20);
        t_username.setBounds(150, 80, 100, 20);
        l_password.setBounds(100, 120, 100, 20);
        t_password.setBounds(150, 120, 100, 20);
        reset.setBounds(130, 160, 60, 20);
        login.setBounds(200, 160, 60, 20);
        //添加清空功能
        reset.addActionListener(e -> {
            t_username.setText("");
            t_password.setText("");
        });
        //登录功能
        login.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String username = t_username.getText();
                String password = t_password.getText();
                if (username.equals("") || password.equals("")) {
                    //用户名密码不能为空
                    JOptionPane.showMessageDialog(null,"用户名或密码不能为空","登录失败",JOptionPane.WARNING_MESSAGE);
                }else if (username.equals("admin") || password.equals("admin")) {
                    //登录成功
                    JOptionPane.showMessageDialog(null,"登录成功","登录成功",JOptionPane.INFORMATION_MESSAGE);

                }else{
                    //登录失败
                    JOptionPane.showMessageDialog(null,"用户名或密码错误","登录失败",JOptionPane.WARNING_MESSAGE);
                }
            }
        });

        //添加控件到jpanel容器中
        panel.add(l_title);
        panel.add(l_username);
        panel.add(l_password);
        panel.add(t_username);
        panel.add(t_password);
        panel.add(reset);
        panel.add(login);

        //jpanel到jfram
        this.add(panel);

        //jframe设置
        this.setTitle("登录页面");
        this.setSize(400,300);
        this.setVisible(true);
        //关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new LoginView();
    }
}
